from sqlalchemy import Column, Integer, String, Text, ForeignKey, DateTime, Boolean
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from ..base import Base


class ProductDetail(Base):
    """
    商品详情模型 - 核心展示信息
    注意：规格、属性、服务、物流等信息已拆分到独立表中
    """
    __tablename__ = "product_details"

    id = Column(Integer, primary_key=True, index=True, comment="详情ID")
    product_id = Column(Integer, ForeignKey("products.id"), nullable=False, comment="商品ID")

    # 核心展示信息
    title = Column(String(200), nullable=True, comment="商品详情标题")
    description = Column(Text, nullable=True, comment="详细描述（富文本）")
    features = Column(Text, nullable=True, comment="商品特点介绍")
    video_url = Column(String(500), nullable=True, comment="商品详情视频URL")

    # SEO优化字段
    meta_title = Column(String(200), nullable=True, comment="SEO标题")
    meta_description = Column(String(500), nullable=True, comment="SEO描述")
    meta_keywords = Column(String(500), nullable=True, comment="SEO关键词")

    # 状态信息
    is_active = Column(Boolean, default=True, comment="是否激活")
    sort_order = Column(Integer, default=0, comment="排序顺序")

    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")

    # 关联商品 - 一对一关系
    product = relationship("Product", backref="detail", uselist=False)

    def __repr__(self):
        return f"<ProductDetail(id={self.id}, product_id={self.product_id}, title='{self.title}')>"


class ProductImage(Base):
    """
    商品图片模型
    """
    __tablename__ = "product_images"

    id = Column(Integer, primary_key=True, index=True, comment="图片ID")
    product_id = Column(Integer, ForeignKey("products.id"), nullable=False, comment="商品ID")
    image_url = Column(String(500), nullable=False, comment="图片URL")
    alt_text = Column(String(200), nullable=True, comment="图片alt描述")
    is_main = Column(Boolean, default=False, comment="是否主图")
    sort_order = Column(Integer, default=0, comment="排序顺序")

    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")

    # 关联商品
    product = relationship("Product", backref="images")

    def __repr__(self):
        return f"<ProductImage(id={self.id}, product_id={self.product_id}, is_main={self.is_main})>"